import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    
    component: Main,
    children:[
      {
        path:'/',
        name:'home',
        component:Home
      },
      {
        path:'/Data',
        name:'Data',
        meta:{isAuth:true,name:'data'},
        component: () => import('../views/Data/Data.vue')
      },
      {
        path:'/ALG',
        name:'ALG',
        component:()=>import('../views/ALG/ALG.vue')
      },
      {
        path:'/Visual',
        name:'Visual',
        component:()=>import('../views/Visual/Visual.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  if (to.meta.name=='data') {
    if(localStorage.getItem('user_name')){
      
      next()
    }else{
      alert('请先登录')
    }
  } else {
    next()
  }
})
router.afterEach((to,from)=>{
  console.log('后置路由守卫',to,from);
  if(to.meta.isAuth){
      document.title=to.meta.title||'DFSY'//修改网页的title
  }else{
      document.title='DFSY'
  }
})
export default router
